#include "dingdanexchangestandarditemmodel.h"

DingdanExchangeStandardItemModel::DingdanExchangeStandardItemModel(QObject *parent) : QStandardItemModel(parent)
{
    header<<"商品型号"
         <<"商品名称"
        <<"单位"
       <<"数量";

    setHorizontalHeaderLabels(header);
}

bool DingdanExchangeStandardItemModel::setData(const QModelIndex &index, const QVariant &value, int role){
    if(index.column() != header.size()-1){
        return false;
    }

    int count = value.toInt();

    DingdanDetail *detail = details[index.row()];
    detail->count = count;

    emit updateItemCount(count);
    return QStandardItemModel::setData(index, count, role);
}

QVariant DingdanExchangeStandardItemModel::data(const QModelIndex &index, int role) const{
    QVariant value = QStandardItemModel::data(index, role);

    //设置内容颜色
    if (role == Qt::TextColorRole && (index.column() == header.size()-1))
        return QVariant::fromValue(QColor(Qt::blue));
    return value;
}

void DingdanExchangeStandardItemModel::reset(){
    clear();
    details.clear();
    products.clear();
    setHorizontalHeaderLabels(header);
}

void DingdanExchangeStandardItemModel::insertItem(QList<DingdanDetail*> details, QList<Product*> products){
    for(int i=0;i<details.size();i++){
        insertItem(details[i], products[i]);
    }
}

void DingdanExchangeStandardItemModel::insertItem(DingdanDetail *detail, Product *product){
    QList<QStandardItem*> rowData;

    rowData.append(new QStandardItem(product==nullptr?"":product->type));
    rowData.append(new QStandardItem(product==nullptr?"":product->name));
    rowData.append(new QStandardItem(product==nullptr?"":product->unit));
    rowData.append(new QStandardItem(detail==nullptr?"":QString("%1").arg(detail->count)));

    appendRow(rowData);

    details.append(detail);
    products.append(product);
}

DingdanDetail* DingdanExchangeStandardItemModel::getDetail(int index){
    if(index<0 || index>= details.size()){
        return nullptr;
    }

    DingdanDetail *detail = details[index];
    if(detail->count == 0){
        return nullptr;
    }

    return detail;
}

Product* DingdanExchangeStandardItemModel::getProduct(int index){
    if(index<0 || index>= products.size()){
        return nullptr;
    }

    Product *product = products[index];
    if(product->productId.isEmpty()){
        return nullptr;
    }

    return product;
}

void DingdanExchangeStandardItemModel::setItemForRow(int row, DingdanDetail *detail, Product *product){
    qDebug()<<detail->count;
    if(detail != nullptr){
        detail->count = detail->count>=0?detail->count:-1*detail->count;
    }
    qDebug()<<detail->count;
    setItem(row, 0, new QStandardItem(product==nullptr?"":product->type));
    setItem(row, 1, new QStandardItem(product==nullptr?"":product->name));
    setItem(row, 2, new QStandardItem(product==nullptr?"":product->unit));
    setItem(row, 3, new QStandardItem(product==nullptr?"":QString("%1").arg(detail->count)));

    details[row] = detail;
    products[row] = product;
}

void DingdanExchangeStandardItemModel::setItemCount(int row, int count){
    setItem(row, 3, new QStandardItem(QString("%1").arg(count)));

    DingdanDetail *detail = details[row];
    detail->count = count;
}

QList<Product*> DingdanExchangeStandardItemModel::getProducts(){
    return products;
}

QList<DingdanDetail*> DingdanExchangeStandardItemModel::getDetails(){
    return details;
}

